package org.jeecg.modules.wms.warehouse.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.util.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.modules.wms.config.WarehouseDictEnum;
import org.jeecg.modules.wms.warehouse.entity.WmsStorageZones;
import org.jeecg.modules.wms.warehouse.entity.WmsWarehouses;
import org.jeecg.modules.wms.warehouse.mapper.WmsStorageZonesMapper;
import org.jeecg.modules.wms.warehouse.service.IWmsStorageZonesService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 储区表
 * @Author: jeecg-boot
 * @Date: 2025-10-25
 * @Version: V1.0
 */
@Service
public class WmsStorageZonesServiceImpl extends ServiceImpl<WmsStorageZonesMapper, WmsStorageZones> implements IWmsStorageZonesService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void enable(String id) {
        boolean empty = ObjectUtil.isEmpty(id);
        if (empty) {
            throw new RuntimeException("参数不能为空");
        }
        WmsStorageZones storageZones = this.getOne(Wrappers.lambdaQuery(WmsStorageZones.class).eq(WmsStorageZones::getId, id));
        if (ObjectUtil.isNull(storageZones)) {
            throw new RuntimeException("仓库不存在");
        }
        if (!WarehouseDictEnum.STATUS_CREATED.getCode().equals(storageZones.getStatus())
                && !WarehouseDictEnum.STATUS_INACTIVE.getCode().equals(storageZones.getStatus())) {
            throw new RuntimeException("仓库状态异常,不能被启用");
        }
        storageZones.setStatus(WarehouseDictEnum.STATUS_ACTIVE.getCode());
        this.updateById(storageZones);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void disable(String id) {
        boolean empty = ObjectUtil.isEmpty(id);
        if (empty) {
            throw new RuntimeException("参数不能为空");
        }
        WmsStorageZones storageZones = this.getOne(Wrappers.lambdaQuery(WmsStorageZones.class).eq(WmsStorageZones::getId, id));
        if (ObjectUtil.isNull(storageZones)) {
            throw new RuntimeException("仓库不存在");
        }
        if (!WarehouseDictEnum.STATUS_ACTIVE.getCode().equals(storageZones.getStatus())) {
            throw new RuntimeException("仓库状态异常,不能被禁用");
        }
        storageZones.setStatus(WarehouseDictEnum.STATUS_INACTIVE.getCode());
        this.updateById(storageZones);
    }

    @Transactional
    @Override
    public void delById(String id) {
        WmsStorageZones wmsStorageZones = this.getOne(Wrappers.lambdaQuery(WmsStorageZones.class).eq(WmsStorageZones::getId, id));
        if (!wmsStorageZones.getStatus().equals(WarehouseDictEnum.STATUS_CREATED.getCode())) {
            throw new RuntimeException("储区异常,不能被删除");
        }
        this.removeById(id);
    }
}
